import java.util.Stack;

public class Test {

    public static void main(String[] args) {
        String S = "({})]";
        System.out.println(isValid(S));
    }

    public static boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        for(int i = 0;i < s.length(); i++){
            //先将字符串拆分
            char ch = s.charAt(i);
            if(ch == '(' || ch == '[' || ch == '{'){
                //依次放入栈中
                stack.push(ch);
            }else{
                if(stack.isEmpty()){
                    return false;
                }
                //栈是先进后出，将栈顶元素与当前元素进行匹配
                char top = stack.peek();
                if(ch == ')' && top == '(' || ch == ']' && top == '[' || ch == '}' && top == '{'){
                    stack.pop();
                }else{
                    return false;
                }
            }
        }
        //若此时栈中还有元素，则返回false
        if(!stack.isEmpty()){
            return false;
        }
        return true;
    }


}
